package com.lb.shopguide.ui.view.chart;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ScaleDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lb.shopguide.R;
import com.lb.shopguide.ui.view.anim.BarAnimation;
import com.lb.shopguide.ui.view.chart.entity.MemberPercentBean;
import com.lb.shopguide.util.lb.BizUtil;

/**
 * Created by kang on 2018/1/11.
 */

public class LbMemberPercentLayout extends LinearLayout {
    private Context mContext;
    private TextView tvShopName;
    private TextView tvMemberNum;
    private ProgressBar progressBar;
    private TextView tvMemberPercent;
    private TextView tvShowMore;

    private int color20 = Color.rgb(148,205,255);
    private int color40 = Color.rgb(255,194,59);
    private int color60 = Color.rgb(163,148,255);
    private int color60p = Color.rgb(255,108,108);


    public LbMemberPercentLayout(Context context) {
        super(context);
        initView(context);
        initListener();
    }

    public LbMemberPercentLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initListener();
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.layout_member_percent_data, this);
        tvShopName = findViewById(R.id.tv_shop_name);
        tvMemberNum = findViewById(R.id.tv_member_number);
        progressBar = findViewById(R.id.progress_bar);
        tvMemberPercent = findViewById(R.id.tv_member_percent);
        tvShowMore = findViewById(R.id.tv_show_more);
    }

    private void initListener() {
        tvShowMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnShowMoreListener != null) {
                    mOnShowMoreListener.showMore();
                }
            }
        });
    }

    public void setMemberPercentBean(MemberPercentBean bean, int pos) {
        tvMemberPercent.setText(bean.getCurPer());
        tvMemberNum.setText(bean.getMemberNum() + "人");
        tvShopName.setText(bean.getShopName());
        progressBar.setMax(100);
        String perStr = BizUtil.getPureNumber(bean.getCurPer());
        int percent = Math.round(Float.parseFloat(perStr));
        if (percent >= 0 && percent <= 20) {
            setProgressBarBack(progressBar, 8, color20);
        }
        if (percent > 20 && percent <= 40) {
            setProgressBarBack(progressBar, 8, color40);
        }
        if (percent > 40 && percent <= 60) {
            setProgressBarBack(progressBar, 8, color60);
        }
        if (percent > 60) {
            setProgressBarBack(progressBar, 8, color60p);
        }
        BarAnimation anim = new BarAnimation(progressBar, 0, percent);
        anim.setDuration(2000);
        progressBar.startAnimation(anim);
    }

    private void setProgressBarBack(ProgressBar bar, int radius, int color) {

        bar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.progress_bar_shape));
        LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
        layerDrawable.mutate();

        GradientDrawable emptyLayer = (GradientDrawable) layerDrawable.getDrawable(0);
        ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);

        emptyLayer.setColor(Color.rgb(240, 240, 240));
        emptyLayer.setCornerRadius(radius);

        GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();

        if (progressLayer != null) {
            progressLayer.setColor(color);
            progressLayer.setCornerRadius(radius);
        }
    }

    public void showMore() {
        tvShowMore.setVisibility(VISIBLE);
    }

    public interface OnShowMoreListener {
        void showMore();
    }

    private OnShowMoreListener mOnShowMoreListener;

    public void setOnShowMoreListener(OnShowMoreListener listener) {
        this.mOnShowMoreListener = listener;
    }
}
